﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace Termites
{
    class Termite
    {
        public const float RIGHT = 0;
        public const float LEFT = 0;
        public const float UP = MathHelper.Pi / 2;
        public const float DOWN = MathHelper.Pi / 2;

        public const float SPEED = 150;

        private int widthTexture;
        private int heightTexture;

        private BoundingBox boundingBox;
        public BoundingBox BoundingBox
        {
            get { return boundingBox; }
            set { boundingBox = value; }
        }

        private bool isPlayer;
        public bool IsPlayer
        {
            get { return isPlayer; }
            set { isPlayer = value; }
        }

        private float direction;
        public float Direction
        {
            get { return direction; }
            set { direction = value; }
        }

        Vector2 position;
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        private Behavior behavior;
        public Behavior Behavior
        {
            get { return behavior; }
            set { behavior = value; }
        }


        public Termite(bool isPlayer, Texture2D tTexture, float scale)
        {
            this.IsPlayer = isPlayer;
            chooseRandomTermiteDirection();
            position = new Vector2(0, 0);
            this.widthTexture = tTexture.Width;
            this.heightTexture = tTexture.Height;
            boundingBox = new BoundingBox(new Vector3(position.X, position.Y, 0),
                                       new Vector3(position.X + widthTexture, position.Y + heightTexture, 0));
        }

        public Termite(bool isPlayer, Vector2 position, Texture2D tTexture, float scale, Behavior behavior) : this(isPlayer, tTexture, scale)
        {
            this.Position = position;
            this.behavior = behavior;
        }

        public void update(GameTime gameTime)
        {
            KeyboardState keyboardState = Keyboard.GetState();
            if (IsPlayer)
            {
                updatePlayerMove(gameTime, keyboardState);
            }
            else
            {
                behavior.move(gameTime, keyboardState, ref direction, ref position);
            }
            boundingBox = new BoundingBox(new Vector3(position.X, position.Y, 0),
                                       new Vector3(position.X + widthTexture, position.Y + heightTexture, 0));
        }

        private void updatePlayerMove(GameTime gameTime, KeyboardState keyboardState)
        {
            //Choose the direction of the movement and update the position
            if (keyboardState.IsKeyDown(Keys.Right))
            {
                direction = Termite.RIGHT;
                position.X += Termite.SPEED * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            else if (keyboardState.IsKeyDown(Keys.Left))
            {
                direction = Termite.LEFT;
                position.X -= Termite.SPEED * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            else if (keyboardState.IsKeyDown(Keys.Up))
            {
                direction = Termite.UP;
                position.Y -= Termite.SPEED * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            else if (keyboardState.IsKeyDown(Keys.Down))
            {
                direction = Termite.DOWN;
                position.Y += Termite.SPEED * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
        }

        private void chooseRandomTermiteDirection()
        {
            Random r = new Random();
            int termiteDirection = r.Next(4);
            switch (termiteDirection)
            {
                case 0:
                    direction = Termite.RIGHT;
                    break;
                case 1:
                    direction = Termite.LEFT;
                    break;
                case 2:
                    direction = Termite.UP;
                    break;
                case 3:
                    direction = Termite.DOWN;
                    break;
            }
        }
    }
}
